import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
/// 消息提示工具类
/// 
/// 功能特点：
/// 1. 支持多种消息类型：加载中、成功、错误、信息、进度条
/// 2. 高度可定制：颜色、动画、位置、遮罩、持续时间等
/// 3. 全局配置与局部覆盖
/// 4. 简洁易用的API设计
class MessageUtil {
  // 全局默认配置
  static MessageUtilConfig _globalConfig = MessageUtilConfig();
  
  /// 初始化全局配置
  /// 
  /// 在应用启动时调用，设置全局默认值
  /// 
  /// 示例：
  /// MessageUtil.initialize(
  ///   backgroundColor: Colors.blueGrey[800]!,
  ///   textColor: Colors.white,
  ///   indicatorColor: Colors.amber,
  ///   maskType: MaskType.black,
  ///   position: ToastPosition.center,
  /// );
  static void initialize({MessageUtilConfig? config}) {
    if (config != null) {
      _globalConfig = config;
    }
    
    // 应用全局配置到EasyLoading
    _applyConfigToEasyLoading(_globalConfig);
  }
  
  /// 显示加载提示
  /// 
  /// [message] 提示文本
  /// [config] 局部配置（可选，覆盖全局配置）
  static void showLoading({
    String message = '加载中...',
    MessageUtilConfig? config,
  }) {
    final mergedConfig = _mergeConfigs(config);
    _applyConfigToEasyLoading(mergedConfig);
    
    EasyLoading.show(
      status: message,
      maskType: _getMaskType(mergedConfig.maskType),
      dismissOnTap: mergedConfig.dismissOnTap,
    );
  }
  
  /// 显示成功提示
  /// 
  /// [message] 提示文本
  /// [duration] 显示时长（可选）
  /// [config] 局部配置（可选，覆盖全局配置）
  static void showSuccess({
    String message = '操作成功',
    Duration? duration,
    MessageUtilConfig? config,
  }) {
    final mergedConfig = _mergeConfigs(config, duration: duration);
    _applyConfigToEasyLoading(mergedConfig);
    
    EasyLoading.showSuccess(
      message,
      duration: mergedConfig.duration,
      maskType: _getMaskType(mergedConfig.maskType),
      dismissOnTap: mergedConfig.dismissOnTap,
    );
  }
  
  /// 显示错误提示
  /// 
  /// [message] 提示文本
  /// [duration] 显示时长（可选）
  /// [config] 局部配置（可选，覆盖全局配置）
  static void showError({
    String message = '发生错误',
    Duration? duration,
    MessageUtilConfig? config,
  }) {
    final mergedConfig = _mergeConfigs(config, duration: duration);
    _applyConfigToEasyLoading(mergedConfig);
    
    EasyLoading.showError(
      message,
      duration: mergedConfig.duration,
      maskType: _getMaskType(mergedConfig.maskType),
      dismissOnTap: mergedConfig.dismissOnTap,
    );
  }
  
  /// 显示信息提示
  /// 
  /// [message] 提示文本
  /// [duration] 显示时长（可选）
  /// [config] 局部配置（可选，覆盖全局配置）
  static void showInfo({
    required String message,
    Duration? duration,
    MessageUtilConfig? config,
  }) {
    final mergedConfig = _mergeConfigs(config, duration: duration);
    _applyConfigToEasyLoading(mergedConfig);
    
    EasyLoading.showInfo(
      message,
      duration: mergedConfig.duration,
      maskType: _getMaskType(mergedConfig.maskType),
      dismissOnTap: mergedConfig.dismissOnTap,
    );
  }
  
  /// 显示进度条
  /// 
  /// [progress] 进度值（0.0 - 1.0）
  /// [message] 提示文本（可选）
  /// [config] 局部配置（可选，覆盖全局配置）
  static void showProgress({
    required double progress,
    String? message,
    MessageUtilConfig? config,
  }) {
    final mergedConfig = _mergeConfigs(config);
    _applyConfigToEasyLoading(mergedConfig);
    
    EasyLoading.showProgress(
      progress,
      status: message,
      maskType: _getMaskType(mergedConfig.maskType),
    );
  }
  
  /// 更新进度条
  /// 
  /// [progress] 进度值（0.0 - 1.0）
  /// [message] 提示文本（可选）
  static void updateProgress({
    required double progress,
    String? message,
  }) {
    EasyLoading.showProgress(progress, status: message);
  }
  
  /// 隐藏当前显示的消息提示
  /// 
  /// [animation] 是否使用消失动画
  static Future<void> dismiss({bool animation = true}) {
    return EasyLoading.dismiss(animation: animation);
  }
  
  /// 合并全局配置和局部配置
  static MessageUtilConfig _mergeConfigs(MessageUtilConfig? localConfig, {Duration? duration}) {
    return MessageUtilConfig(
      // 时间配置：优先使用局部duration，其次使用传入的duration，最后使用全局duration
      duration: duration ?? localConfig?.duration ?? _globalConfig.duration,
      
      // 遮罩配置
      maskType: localConfig?.maskType ?? _globalConfig.maskType,
      maskColor: localConfig?.maskColor ?? _globalConfig.maskColor,
      
      // 交互配置
      dismissOnTap: localConfig?.dismissOnTap ?? _globalConfig.dismissOnTap,
      userInteractions: localConfig?.userInteractions ?? _globalConfig.userInteractions,
      
      // 样式配置
      backgroundColor: localConfig?.backgroundColor ?? _globalConfig.backgroundColor,
      textColor: localConfig?.textColor ?? _globalConfig.textColor,
      indicatorColor: localConfig?.indicatorColor ?? _globalConfig.indicatorColor,
      progressColor: localConfig?.progressColor ?? _globalConfig.progressColor,
      fontSize: localConfig?.fontSize ?? _globalConfig.fontSize,
      radius: localConfig?.radius ?? _globalConfig.radius,
      
      // 位置配置
      position: localConfig?.position ?? _globalConfig.position,
      
      // 动画配置
      animationStyle: localConfig?.animationStyle ?? _globalConfig.animationStyle,
      animationDuration: localConfig?.animationDuration ?? _globalConfig.animationDuration,
    );
  }
  
  /// 将配置应用到EasyLoading实例
  static void _applyConfigToEasyLoading(MessageUtilConfig config) {
    EasyLoading.instance
      ..displayDuration = config.duration
      ..indicatorType = config.indicatorType
      ..loadingStyle = EasyLoadingStyle.custom
      ..maskType = _getMaskType(config.maskType)
      ..maskColor = config.maskColor ?? Colors.black.withOpacity(0.5)
      ..userInteractions = config.userInteractions
      ..dismissOnTap = config.dismissOnTap
      ..toastPosition = _getPosition(config.position)
      ..animationStyle = _getAnimationStyle(config.animationStyle)
      ..animationDuration = config.animationDuration
      ..backgroundColor = config.backgroundColor ?? Colors.grey[850]!
      ..textColor = config.textColor ?? Colors.white
      ..indicatorColor = config.indicatorColor ?? Colors.white
      ..progressColor = config.progressColor ?? Colors.white
      ..fontSize = config.fontSize
      ..radius = config.radius;
  }
  
  /// 转换遮罩类型枚举
  static EasyLoadingMaskType _getMaskType(MaskType maskType) {
    switch (maskType) {
      case MaskType.none:
        return EasyLoadingMaskType.none;
      case MaskType.clear:
        return EasyLoadingMaskType.clear;
      case MaskType.black:
        return EasyLoadingMaskType.black;
      case MaskType.custom:
        return EasyLoadingMaskType.custom;
    }
  }
  
  /// 转换位置枚举
  static EasyLoadingToastPosition _getPosition(ToastPosition position) {
    switch (position) {
      case ToastPosition.top:
        return EasyLoadingToastPosition.top;
      case ToastPosition.center:
        return EasyLoadingToastPosition.center;
      case ToastPosition.bottom:
        return EasyLoadingToastPosition.bottom;
      case ToastPosition.custom:
        return EasyLoadingToastPosition.center; // 自定义位置通过offset实现
    }
  }
  
  /// 转换动画样式枚举
  static EasyLoadingAnimationStyle _getAnimationStyle(MessageAnimationStyle animationStyle) {
    switch (animationStyle) {
      case MessageAnimationStyle.fade:
        return EasyLoadingAnimationStyle.opacity;
      case MessageAnimationStyle.scale:
        return EasyLoadingAnimationStyle.scale;
      case MessageAnimationStyle.slide:
        return EasyLoadingAnimationStyle.offset;
      case MessageAnimationStyle.rotation:
        return EasyLoadingAnimationStyle.opacity; // 旋转动画需要自定义实现
      case MessageAnimationStyle.custom:
        return EasyLoadingAnimationStyle.opacity; // 自定义动画需要额外处理
    }
  }
}

/// ==============================
/// 配置类与枚举定义
/// ==============================

/// 消息提示配置类
class MessageUtilConfig {
  /// 提示显示时长
  final Duration duration;
  
  /// 遮罩类型
  final MaskType maskType;
  
  /// 自定义遮罩颜色（仅当maskType为custom时有效）
  final Color? maskColor;
  
  /// 点击提示框是否消失
  final bool dismissOnTap;
  
  /// 显示提示时是否允许用户交互
  final bool userInteractions;
  
  /// 提示框背景颜色
  final Color? backgroundColor;
  
  /// 文本颜色
  final Color? textColor;
  
  /// 指示器颜色（加载动画）
  final Color? indicatorColor;
  
  /// 进度条颜色
  final Color? progressColor;
  
  /// 文字大小
  final double fontSize;
  
  /// 提示框圆角
  final double radius;
  
  /// 提示位置
  final ToastPosition position;

  
  /// 动画样式
  final MessageAnimationStyle animationStyle;
  
  /// 动画时长
  final Duration animationDuration;
  
  /// 指示器类型
  final EasyLoadingIndicatorType indicatorType;
  
  MessageUtilConfig({
    this.duration = const Duration(seconds: 2),
    this.maskType = MaskType.none,
    this.maskColor,
    this.dismissOnTap = false,
    this.userInteractions = true,
    this.backgroundColor,
    this.textColor,
    this.indicatorColor,
    this.progressColor,
    this.fontSize = 14.0,
    this.radius = 10.0,
    this.position = ToastPosition.center,
    this.animationStyle = MessageAnimationStyle.fade,
    this.animationDuration = const Duration(milliseconds: 200),
    this.indicatorType = EasyLoadingIndicatorType.fadingCircle,
  });
}

/// 遮罩类型枚举
enum MaskType {
  none,       // 无遮罩
  clear,      // 透明遮罩（阻止交互）
  black,      // 黑色半透明遮罩
  custom,     // 自定义遮罩
}

/// 提示位置枚举
enum ToastPosition {
  top,         // 顶部
  center,      // 居中
  bottom,      // 底部
  custom,      // 自定义位置
}

/// 动画样式枚举
enum MessageAnimationStyle {
  fade,       // 淡入淡出
  scale,      // 缩放
  slide,      // 滑动
  rotation,   // 旋转
  custom,     // 自定义动画
}
